﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Globalization;

namespace TheTVDB
{
    public class Series
    {
        public int ID { get; set; }
        public List<string> Actors { get; private set; }
        public DayOfWeek AirsDayOfWeek { get; set; }
        public DateTime AirTime { get; set; }
        public string ContentRating { get; set; }
        public DateTime FirstAired { get; set; }
        public List<string> Genres { get; private set; }
        public string ImdbID { get; set; }
        public string Language { get; set; }
        public string Network { get; set; }
        public string Overview { get; set; }
        public float Rating { get; set; }
        public int Runtime { get; set; }
        public int SeriesID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public string BannerPath { get; set; }
        public string FanartPath { get; set; }
        public List<Banner> Banners { get; set; }
        public long LastUpdated { get; set; }
        public string Zap2ItID { get; set; }

        public List<Episode> Episodes { get; private set; }

        public Series()
        {
            Actors = new List<string>();
            Genres = new List<string>();
            Episodes = new List<Episode>();
            Banners = new List<Banner>();
        }

        public static Series FromXmlDocument(XmlDocument document)
        {
            Series s = Series.FromXml((XmlElement)document.SelectSingleNode("//Series"));
            foreach (XmlElement ep in document.SelectNodes("//Episode"))
            {
                s.Episodes.Add(Episode.FromXml(ep));
            }
            return s;
        }

        public static Series FromXml(XmlElement seriesNode)
        {
            Series s = new Series();
            foreach (XmlElement c in seriesNode.ChildNodes)
            {
                if (c.InnerText == "")
                    continue;
                switch (c.Name)
                {
                    case "id":
                        int id;
                        if(Int32.TryParse(c.InnerText, out id))
                            s.ID = id;
                        break;
                    case "Actors":
                        s.Actors.AddRange(c.InnerText.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));

                        for (int i = 0; i < s.Actors.Count; i++)
                        {
                            s.Actors[i] = s.Actors[i].Trim();
                        }
                        break;
                    case "Airs_DayOfWeek":
                        try
                        {
                            s.AirsDayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), c.InnerText);
                        }
                        catch { }
                        break;
                    case "Airs_Time":
                        DateTime dt;
                        if (DateTime.TryParse(c.InnerText, out dt))
                            s.AirTime = dt;
                        break;
                    case "ContentRating":
                        s.ContentRating = c.InnerText;
                        break;
                    case "FirstAired":
                        if (DateTime.TryParse(c.InnerText, out dt))
                            s.FirstAired = dt;
                        break;
                    case "Genre":
                        s.Genres.AddRange(c.InnerText.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
                        for (int i = 0; i < s.Genres.Count; i++)
                        {
                            s.Genres[i] = s.Genres[i].Trim();
                        }
                        break;
                    case "IMDB_ID":
                        s.ImdbID = c.InnerText;
                        break;
                    case "Language":
                        s.Language = c.InnerText;
                        break;
                    case "Network":
                        s.Network = c.InnerText;
                        break;

                    case "Overview":
                        s.Overview = c.InnerText;
                        break;
                    case "Rating":
                        float f;
                        if (Single.TryParse(c.InnerText, NumberStyles.Float, CultureInfo.GetCultureInfo("en-US"), out f))
                            s.Rating = f;
                        break;
                    case "Runtime":
                        if (Int32.TryParse(c.InnerText, out id))
                            s.Runtime = id;
                        break;
                    case "SeriesID":
                        if (Int32.TryParse(c.InnerText, out id))
                            s.SeriesID = id;
                        break;
                    case "SeriesName":
                        s.Name = c.InnerText;
                        break;
                    case "Status":
                        s.Status = c.InnerText;
                        break;
                    case "banner":
                        s.BannerPath = c.InnerText;
                        break;
                    case "fanart":
                        s.FanartPath = c.InnerText;
                        break;
                    case "lastupdated":
                        long l;
                        if (Int64.TryParse(c.InnerText, out l))
                            s.LastUpdated = l;
                        break;
                    case "zap2it_id":
                        s.Zap2ItID = c.InnerText;
                        break;
                }
            }
            return s;
        }

    }
}
